package com.ling.controller;

import com.ling.bean.Msg;
import com.ling.bean.User;
import com.ling.bean.UserBlogType;
import com.ling.service.BlogTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class BlogTypeController {
    private final BlogTypeService blogTypeService;

    @Autowired
    public BlogTypeController(BlogTypeService blogTypeService) {
        this.blogTypeService = blogTypeService;
    }

    //跳转到博客分类管理页面
    @RequestMapping(value = "/blog-type", method = RequestMethod.GET)
    public String goToMyBlogType() {
        return "/blog-type";
    }

    //查询所有的博客分类
    @RequestMapping(value = "/getAllUserBlogType", method = RequestMethod.GET)
    @ResponseBody
    public Msg getAllUserBlogType(@Autowired HttpSession session) {
        User user = (User) session.getAttribute("user");
        //判空
        if (user == null || user.getId() == null) {
            return Msg.fail().add("error", "session失效，请重新登录");
        }
        //拿到id，开始查询
        List<UserBlogType> userBlogTypeList = blogTypeService.getUserBlogType(user.getId());
        return Msg.success().add("userBlogTypeList", userBlogTypeList);
    }

    //添加用户博客类别
    @RequestMapping(value = "/addUserBlogType", method = RequestMethod.POST)
    @ResponseBody
    public Msg addUserBlogType(@RequestParam String id, @RequestParam String blogTypeName, @Autowired HttpSession session) {
        //判断用户
        User user = (User) session.getAttribute("user");
        if (user == null) {
            //未登录
            return Msg.fail().add("error", "session失效，请重新登录");
        }
        UserBlogType userBlogType = new UserBlogType();
        //设置id
        userBlogType.setId(id);
        userBlogType.setUserid(user.getId());
        userBlogType.setBlogtypename(blogTypeName);
        userBlogType.setBlognum(0);
        blogTypeService.addBlogType(userBlogType);
        return Msg.success();
    }

    //删除指定id的博客类别,当文章数不为0时，不可删除，需要先将文章均删除之后再删除该博客类别
    @RequestMapping(value = "/delBlogType", method = RequestMethod.POST)
    @ResponseBody
    public Msg delBlogType(@Autowired HttpSession session, @RequestParam String blogTypeId) {
        //判断用户
        User user = (User) session.getAttribute("user");
        if (user == null) {
            //未登录
            return Msg.fail().add("error", "session失效，请重新登录");
        }
        //判断,文章数不为0时，不可删除，需要先将文章均删除之后再删除该博客类别
        if (blogTypeService.getCountOfBlogType(blogTypeId) > 0) {
            return Msg.fail().add("error", "该博客类别下还有文章，不能删除，请先清空该类别下的所有文章！");
        }
        //可以删除
        blogTypeService.delBlogTypeById(blogTypeId);
        return Msg.success();
    }

    //修改博客类别
    @RequestMapping(value = "/updateBlogType", method = RequestMethod.POST)
    @ResponseBody
    public Msg updateBlogType(@Autowired HttpSession session,@RequestParam String blogTypeName,@RequestParam String id) {
        //判断用户
        User user = (User) session.getAttribute("user");
        if (user == null) {
            //未登录
            return Msg.fail().add("error", "session失效，请重新登录");
        }
        //开始修改
        UserBlogType userBlogType = new UserBlogType();
        userBlogType.setId(id);
        userBlogType.setBlogtypename(blogTypeName);
        blogTypeService.updateBlogType(userBlogType);
        return Msg.success();
    }

}
